package io.lvdaxian.mybatis13.mapping;


import io.lvdaxian.mybatis13.mapping.BoundSql;
import io.lvdaxian.mybatis13.mapping.ResultMap;
import io.lvdaxian.mybatis13.mapping.SqlCommandType;
import io.lvdaxian.mybatis13.mapping.SqlSource;
import io.lvdaxian.mybatis13.scripting.LanguageDriver;
import io.lvdaxian.mybatis13.session.Configuration;

import java.util.List;

/**
 * sql片段的 statement
 *
 * @author lihh
 */
public class MappedStatement {
  
  private Configuration configuration;
  private String id;
  private SqlCommandType sqlCommandType;
  private SqlSource sqlSource;
  Class<?> resultType;
  private LanguageDriver lang;
  private List<ResultMap> resultMaps;
  
  MappedStatement() {
    // constructor disabled
  }
  
  /**
   * 建造者
   */
  public static class Builder {
    
    private MappedStatement mappedStatement = new MappedStatement();
    
    public Builder(Configuration configuration, String id, SqlCommandType sqlCommandType, SqlSource sqlSource, Class<?> resultType) {
      mappedStatement.configuration = configuration;
      mappedStatement.id = id;
      mappedStatement.sqlCommandType = sqlCommandType;
      mappedStatement.sqlSource = sqlSource;
      mappedStatement.resultType = resultType;
      mappedStatement.lang = configuration.getDefaultScriptingLanguageInstance();
    }
    
    public MappedStatement build() {
      assert mappedStatement.configuration != null;
      assert mappedStatement.id != null;
      return mappedStatement;
    }
    
    public String id() {
      return mappedStatement.id;
    }
    
    public Builder resultMaps(List<ResultMap> resultMaps) {
      mappedStatement.resultMaps = resultMaps;
      return this;
    }
    
  }
  
  public Configuration getConfiguration() {
    return configuration;
  }
  
  public String getId() {
    return id;
  }
  
  public SqlCommandType getSqlCommandType() {
    return sqlCommandType;
  }
  
  public SqlSource getSqlSource() {
    return sqlSource;
  }
  
  public Class<?> getResultType() {
    return resultType;
  }
  
  public LanguageDriver getLang() {
    return lang;
  }
  
  public List<ResultMap> getResultMaps() {
    return resultMaps;
  }
  
  public BoundSql getBoundSql(Object parameterObject) {
    return this.sqlSource.getBoundSql(parameterObject);
  }
  
}
